/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.applets.regula;

import com.Ap;
import com.Klient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import org.applets.Dostep;

/**
 *
 * @author Michał
 */
public class Aktualizator extends Thread{

    private List<com.Ap> listaAp;
    private Boolean stop;
    private Map<String,List<String>> mapaApClient;
    private DefaultComboBoxModel comboModel;
    private static int STOP_TIME =10000;

    public Aktualizator(Map<String,List<String>> mapaApClient, DefaultComboBoxModel comboModel1) {
        this.mapaApClient=mapaApClient;
        this.comboModel = comboModel1;
        listaAp = Dostep.pobierzSieci();
        
    }
    
    
    @Override
    public void run() {
        super.run(); 
        stop=false;
        while(!stop)
        {
            try {
                listaAp = Dostep.pobierzSieci();
                for (Ap ap : listaAp) {
                    if(mapaApClient.containsKey(ap.getBSSID()))
                    {
                        List<String> listaKl = mapaApClient.get(ap.getBSSID());
                        for (Klient klient : ap.getListaKlientow()) {
                            if(!listaKl.contains(klient.getMAC()))
                                listaKl.add(klient.getMAC());
                        }
                    }else
                    {
                        List<String> listaKl = new ArrayList<String>();
                        for (Klient klient : ap.getListaKlientow()) {
                            listaKl.add(klient.getMAC());
                        }
                        mapaApClient.put(ap.getBSSID(), listaKl);
                    }
                }
                aktualizujModel();
                synchronized(this){
                this.wait(STOP_TIME);
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(Aktualizator.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void aktualizujModel() {

        if(mapaApClient!=null && !mapaApClient.isEmpty())
        {
            comboModel.removeAllElements();
            for (String ap : mapaApClient.keySet()) {
                comboModel.addElement(ap);
            }
            comboModel.addElement("FF:FF:FF:FF:FF:FF");
            
        }
    }
    
    
    
}
